Author

Olivia Gilpin

USGS Data

library(dataRetrieval)
library(dplyr)  

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(tsibble) 
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr

Attaching package: 'tsibble'
The following objects are masked from 'package:base':

    intersect, setdiff, union
library(feasts)   
Loading required package: fabletools
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.0     ✔ readr     2.1.5
✔ ggplot2   3.5.1     ✔ stringr   1.5.1
✔ lubridate 1.9.4     ✔ tibble    3.2.1
✔ purrr     1.0.2     ✔ tidyr     1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter()       masks stats::filter()
✖ lubridate::interval() masks tsibble::interval()
✖ dplyr::lag()          masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
poudre_flow <- readNWISdv(siteNumber = "06752260",    
                          parameterCd = "00060",      
                          startDate = "2013-01-01",   
                          endDate = "2023-12-31") |>  
  renameNWISColumns() |>                              
  mutate(Date = yearmonth(Date)) |>                   
  group_by(Date) |>                                   
  summarise(Flow = mean(Flow))                        
GET:https://waterservices.usgs.gov/nwis/dv/?site=06752260&format=waterml%2C1.1&ParameterCd=00060&StatCd=00003&startDT=2013-01-01&endDT=2023-12-31
head(poudre_flow)
# A tibble: 6 × 2
      Date   Flow
     <mth>  <dbl>
1 2013 Jan  18.1 
2 2013 Feb  18.0 
3 2013 Mar   8.21
4 2013 Apr   5.94
5 2013 May 333.  
6 2013 Jun 300.  

1. Convert to tsibble

poudre_tsbl <- poudre_flow |>
  as_tsibble(index = Date)

poudre_tsbl
# A tsibble: 132 x 2 [1M]
       Date    Flow
      <mth>   <dbl>
 1 2013 Jan   18.1 
 2 2013 Feb   18.0 
 3 2013 Mar    8.21
 4 2013 Apr    5.94
 5 2013 May  333.  
 6 2013 Jun  300.  
 7 2013 Jul   75.6 
 8 2013 Aug   48.8 
 9 2013 Sep 1085.  
10 2013 Oct  146.  
# ℹ 122 more rows

2. Plotting the time series

library(ggplot2)
library(plotly)

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
poudre_plot <- poudre_tsbl |>
  ggplot(aes(x = Date, y = Flow)) +
  geom_line(color = "purple", linewidth = 0.8) +
  labs(title = "Monthly Average Streamflow in Cache la Poudre River (2013-2023)",
       x = "Date",
       y = "Flow (cfs)") +
  theme_minimal()

poudre_plot

ggplotly(poudre_plot)

3. Subseries

gg_subseries(poudre_tsbl) +
  labs(title = "Monthly Cache la Poudre River Flow Patterns", 
       y = "Flow (cfs)", 
       x = "Year") + 
  theme_minimal()
Plot variable not specified, automatically selected `y = Flow`

Describe what you see in the plot. How are “seasons” defined in this plot? What do you think the “subseries” represent?

Looking at the subseries plot of the Cache la Poudre River, I am able to see a natural rhythm where the river flow has significant peaks in May and June, reaching just above 2000 cfs, before it gradually declines throughout the summer and remains low during winter months. In this plot, “seasons” are defined as calendar months (January through December), with each panel showing how that particular month’s flow patterns changed across the decade of data. The subseries represent the river’s behavior during each specific month across multiple years (2013-2023), which reveals both the river’s consistent seasonal pattern and the year-to-year variations within each month, particularly during the high-flow periods when snowmelt dominates the river’s character.

4. Decompose

poudre_dcmp <- poudre_tsbl |>
  model(stl = STL(Flow ~ trend(window = 21) + season(window = 13)))

components(poudre_dcmp) |>
  autoplot() +
  labs(title = "STL Decomposition of Cache la Poudre River Flow")

Describe what you see in the plot. How do the components change over time? What do you think the trend and seasonal components represent?